<template>
  <div class="pd30 produceFactory">
    <div class="panelWrap">
      <div class="title flex flexac flexjcsb fs14 cfff fwb pl p-r-20">
        <div>电梯使用单位</div>
        <!-- <el-button class="addBtn fw defaultbtn"  type="primary">添加</el-button> -->
      </div>
      <div class="con">
        <div class="con-row flex flexac pl search-box">
          <!-- <div class="inputLable inputLable1 flex flexac">
            <el-input v-model="queryParam.id" class="input" placeholder="请输入电梯ID..." />
          </div> -->
          <div class="inputLable inputLable2 flex flexac">
            <el-input v-model="queryParam.elevatorId" class="input" placeholder="请输入电梯Id..." />
          </div>
          <div class="groupbtn flex m-l-60 ">
            <div class="selectBtn flex flexac " @click="getElevatorList">查询</div>
            <el-button class="emptyBtn flexcenter m-l-25" type="info" @click="handleEmptySearch"><i class="iconfont icon-qingkong" />清空</el-button>
          </div>
        </div>
        <div class="con-row flex flexac flexjcsb pl">
          <el-button class="defaultbtn" type="primary" @click="handleRefresh"><i class="el-icon-refresh" :class="refresh?'goRefresh':''" />刷新</el-button>
          <div class="m-r-25 flex">
            <el-button class="defaultbtn none" @click="handleAllSelectBtn"><i class="iconfont icon-quanxuan" />全选</el-button>
            <el-button class="defaultbtn none" @click="handleEmptySelectBtn"><i class="el-icon-circle-close" />取消</el-button>
            <el-button class="defaultbtn" type="primary" @click="handleAdd"><i class="el-icon-circle-plus-outline" />添加</el-button>
            <el-button type="danger" class="defaultbtn" @click="handleDeleteBtn"><i class="el-icon-delete-solid" />删除</el-button>
            <el-button class="defaultbtn" type="primary" @click="handleExportExcel"><i class="iconfont icon-wenjiandaochu" />导出</el-button>
            <div class="pre m-l-10">
              <el-button class="defaultbtn pre" type="primary" @click="handleSenior('field')"><i class="iconfont icon-shezhi" />字段
              </el-button>
              <div class="selectUl" :class="seniorSet.field?'showSelectUl':'hideSelectUl'">
                <div v-for="(item,i) in tablehead" :key="i" class="li">
                  <el-checkbox v-model="item.isShow">{{ item.label }}</el-checkbox>
                </div>
              </div>
            </div>
            <div class="pre m-l-10">
              <el-button class="defaultbtn" type="primary" @click="handleSenior('sortable')"><i class="iconfont icon-shezhi" />排序</el-button>
              <div class="selectUl" :class="seniorSet.sortable?'showSelectUl':'hideSelectUl'">
                <div v-for="(item,i) in tablehead" v-if="item.isShow" :key="i" class="li">
                  <el-checkbox v-model="item.sortable">{{ item.label }}</el-checkbox>
                </div>
              </div>
            </div>
            <!-- <el-button class="defaultbtn" @click="handleExportExcel" type="primary"><i class="iconfont icon-shezhi"></i>固定</el-button> -->
          </div>
        </div>

        <el-table id="factorytable" ref="factorytable" class="con-table" type="expand" :data="factorytable" style="width: 100%" @select="handleOneSelect" @select-all="handleAllSelect">
          <el-table-column width="90" type="selection" label="全选" />

          <template v-for="item in tablehead">
            <el-table-column v-if="item.isShow" :key="item.index" align="center" :sortable="item.sortable" :show-overflow-tooltip="true" :prop="item.prop" :label="item.label" />
          </template>

          <el-table-column prop="name" label="基本操作">
            <template slot-scope="scope">
              <el-button class="defaultbtn" type="primary" plain size="mini" @click="handleEdit(scope.$index, scope.row)">
                <i class="el-icon-edit" />
                编辑</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination class="paginationWrap flex " :current-page="paging.currentPage" :page-sizes="paging.pageSizeArr" :page-size="paging.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="paging.total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
      </div>
    </div>
    <el-dialog class="bgtransparent" :visible.sync="elevatorDialog">
      <elevatorDialog :data="editFactory" :is-edit="isEdit" @handleRefresh="handleRefresh" @handleCloseDialog="handleCloseDialog" />
    </el-dialog>
  </div>
</template>

<script>
import elevatorDialog from './dialog'
import { fetchList, fetchDelete } from '@/api/elevator/elevatoruseunit'
import { showNotice, formatDateTime } from '@/utils/toolclass'

export default {
  name: 'Unnamed',
  components: {
    elevatorDialog
  },
  props: {},
  data() {
    return {
      paging: {
        currentPage: 1,
        pageSize: 100,
        pageSizeArr: [10, 20, 50, 100],
        total: 400
      },

      factorytable: [
        {
          id: 1,
          elevatorId: 1,
          elevatorName: '测试',
          unitId: 1,
          unitName: '测试',
          unitType: 0,
          version: 1
        },
        {
          id: 1,
          elevatorId: 1,
          elevatorName: '测试',
          unitId: 1,
          unitName: '测试',
          unitType: 0,
          version: 1
        }
      ],
      queryParam: {
        id: '',
        elevatorId: ''
      },
      elevatorDialog: false,
      factoryListInfo: [],
      editFactory: {},
      isEdit: false,
      selectDataLength: 0,
      refresh: false,
      seniorSet: {
        'field': false,
        'sortable': false
      },
      tablehead: [
        {
          index: 0,
          prop: 'elevatorId',
          label: '电梯ID',
          sortable: false,
          fixed: true,
          isShow: true
        },
        {
          index: 1,
          prop: 'elevatorName',
          label: '电梯名称',
          sortable: false,
          fixed: true,
          isShow: true
        },
        {
          index: 2,
          prop: 'unitId',
          label: '使用单位ID',
          sortable: false,
          fixed: true,
          isShow: true
        },
        {
          index: 3,
          prop: 'unitName',
          label: '使用单位名称',
          sortable: false,
          fixed: true,
          isShow: true
        },
        {
          index: 4,
          prop: 'unitType',
          label: '使用单位类型',
          sortable: false,
          fixed: true,
          isShow: true
        }
      ]

    }
  },
  computed: {},
  watch: {},
  created() {
    this.getElevatorList()
  },
  methods: {
    handleSenior(type) {
      if (type === 'field') {
        this.seniorSet.field = !this.seniorSet.field
      }
      if (type === 'sortable') {
        this.seniorSet.sortable = !this.seniorSet.sortable
      }
    },

    handleExportExcel() {
      require.ensure([], () => {
        const { export_json_to_excel } = require('@/excel/Export2Excel')
        const tHeader = ['电梯ID', '电梯名称', '使用单位ID', '使用单位名称', '使用单位类型']
        const filterVal = ['elevatorId', 'elevatorName', 'unitId', 'unitName', 'unitType']
        const data = this.formatJson(filterVal, this.factorytable)
        export_json_to_excel(tHeader, data, '电梯使用单位列表(' + formatDateTime().ymd) + ')'
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    handleRefresh() {
      this.refresh = true
      this.getElevatorList()
      setTimeout(() => {
        this.refresh = false
      }, 1200)
    },
    getElevatorList() {
      fetchList(this.queryParam.id, this.queryParam.elevatorId, this.paging.currentPage, this.paging.pageSize).then((res) => {
        console.log(res)

        // if (res.code === '000000') {
        //   this.factoryListInfo = res
        //   this.factorytable = res.data
        // this.paging = {
        //   currentPage: res.pager.pageIndex,
        //   pageSize: res.pager.pageSize,
        //   total: res.pager.totalSize
        // }
        // }
      })
    },
    handleEmptySearch() {
      this.queryParam.id = ''
      this.queryParam.elevatorId = ''
      this.getElevatorList()
    },
    handleEdit(index, row) {
      this.elevatorDialog = true
      this.isEdit = true
      this.editFactory = {
        id: row.id,
        elevatorId: row.elevatorId,
        elevatorName: row.elevatorName,
        unitId: row.unitId,
        unitName: row.unitName,
        unitType: row.unitType === 0 ? '物业单位' : row.unitType === 1 ? '项目建设单位' : row.unitType === 2 ? '所有权人' : '其他单位 ',
        version: row.version
      }
    },
    handleAdd() {
      this.elevatorDialog = true
      this.isEdit = false
      this.editFactory = {
        id: '',
        elevatorId: '',
        elevatorName: '',
        unitId: '',
        unitName: '',
        unitType: '',
        version: ''
      }
    },
    handleDeleteBtn() {
      console.log(this.selectDataLength)
      if (this.selectDataLength <= 0) {
        // this.$alert('还没有选中项', '提示', {
        //   confirmButtonText: '确定',
        //   callback: action => {
        //   }
        // })
        showNotice('error', '提示', '还没有选中项')
      } else if (this.selectDataLength.length > 1) {
        showNotice('error', '提示', '不允许多行删除')
      } else {
        this.$alert('这个操作会删除和它相关联的所有数据,确定要删除吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          showCancelButton: true,
          callback: action => {
            if (action === 'confirm') {
              this.handleDelete()
            }
          }
        })
      }
    },
    handleDelete() {
      const selectDataId = this.selectDataLength[0].id
      fetchDelete(selectDataId).then((res) => {
        if (res.code === '000000') {
          showNotice('success', '提示', '删除成功')
          this.getElevatorList()
        }
      })
    },
    handleSelectVal() {
      const _selectData = this.$refs.factorytable.selection
      this.selectDataLength = _selectData
    },
    handleCloseDialog() {
      this.elevatorDialog = !this.elevatorDialog
    },
    handleOneSelect(val) {
      console.log('选中某一行')
      console.log(val)
      this.handleSelectVal()
    },
    handleAllSelect(val) {
      console.log('全选')
      console.log(val)
    },
    handleAllSelectBtn() {
      console.log('全选操作')
      console.log(this.$refs.table)
      this.$refs.factorytable.toggleAllSelection()
    },
    handleEmptySelectBtn() {
      console.log('全选操作')
      console.log(this.$refs.table)
      this.$refs.factorytable.clearSelection()
    },
    handleSizeChange(val) {
      this.paging.pageSize = val
      this.getElevatorList()
    },
    handleCurrentChange(val) {
      this.paging.currentPage = val
      this.getElevatorList()
    }
  }
}
</script>

<style scoped lang="scss">

</style>
